package com.novellotus.iov.carvice.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.bean.RxEinfos;
import com.novellotus.iov.carvice.utils.MyViewUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2017/12/23.
 *
 */

public class HomePageAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<RxEinfos> rxEinfosList;
    private HomePageEnterpriseAdapter.EnterpriseClickInterface enterpriseClickInterface;

    public HomePageAdapter(Context context,List<RxEinfos> list, HomePageEnterpriseAdapter.EnterpriseClickInterface enterpriseClickInterface) {
        this.context = context;
        this.rxEinfosList = list;
        this.enterpriseClickInterface = enterpriseClickInterface;
    }


    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (rxEinfosList != null && rxEinfosList.size() > 0) {
            switch (viewType) {
                case 0:
                    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_home_page_function, null);
                    return new FunctionHolder(view);
                case 1:
                    View viewEnterprise = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_home_page_enterprise, null);
                    return new EnterpriseHolder(viewEnterprise);
                case 2:
                    View viewTips = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_home_page_tips, null);
                    return new TipsHolder(viewTips);
            }
        } else {
            switch (viewType) {
                case 0:
                    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_home_page_function, null);
                    return new FunctionHolder(view);
                case 1:
                    View viewTips = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_home_page_tips, null);
                    return new TipsHolder(viewTips);
            }
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (rxEinfosList != null && rxEinfosList.size() > 0) {
            switch (position) {
                case 0:
                    initFunction((FunctionHolder) holder);
                    break;
                case 1:
                    initEnterprise((EnterpriseHolder) holder);
                    break;
                case 2:
                    initTips((TipsHolder) holder);
                    break;
            }
        } else {
            switch (position) {
                case 0:
                    initFunction((FunctionHolder) holder);
                    break;
                case 1:
                    initTips((TipsHolder) holder);
                    break;
            }
        }

    }

    private void initFunction(FunctionHolder functionHolder) {
        List<Integer> list = new ArrayList<>();
        list.add(R.mipmap.banner_home1);
        list.add(R.mipmap.banner_home2);
        list.add(R.mipmap.banner_home3);
        list.add(R.mipmap.banner_home4);
        functionHolder.rollpager.setAdapter(new HomePagerLoopAdapter(functionHolder.rollpager, list));
        functionHolder.rollpager.setHintView(new ColorPointHintView(context, Color.parseColor("#2eb9fc"), Color.WHITE));
        functionHolder.recyclerView.setLayoutManager(MyViewUtils.gridLayoutManager(context, 4));
        functionHolder.recyclerView.setAdapter(new HomePageFunctionAdapter(context));
    }

    private void initEnterprise(EnterpriseHolder enterpriseHolder) {
        if (rxEinfosList == null || rxEinfosList.size() == 0) {
            enterpriseHolder.rlt_item.setVisibility(View.GONE);
        } else {
            enterpriseHolder.rlt_item.setVisibility(View.VISIBLE);
            enterpriseHolder.recyclerView.setLayoutManager(MyViewUtils.getLinearLayoutManager(context));
            enterpriseHolder.recyclerView.setAdapter(new HomePageEnterpriseAdapter(rxEinfosList, enterpriseClickInterface));
        }

    }

    private void initTips(TipsHolder tipsHolder) {
        tipsHolder.recyclerView.setLayoutManager(MyViewUtils.getLinearLayoutManager(context));
        tipsHolder.recyclerView.setAdapter(new HomePageTipsAdapter(context));
    }

    @Override
    public int getItemCount() {
        if (rxEinfosList != null && rxEinfosList.size() > 0) {
            return 3;
        } else {
            return 2;
        }

    }

    /**
     * banner和功能视图
     */
    class FunctionHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rollpager)
        RollPagerView rollpager;

        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;

        FunctionHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 企业视图
     */
    class EnterpriseHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        @BindView(R.id.rlt_item)
        RelativeLayout rlt_item;

        EnterpriseHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    /**
     * 贴士视图
     */
    class TipsHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;

        TipsHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
